var del = require('del'),
    gulp = require('gulp'),
    sftp = require('gulp-sftp'),
    zip = require('gulp-zip'),
    watch = require('gulp-watch'),
    concat = require('gulp-concat'),
    rename = require("gulp-rename"),
    uglify = require('gulp-uglify'),
    htmlmin = require('gulp-htmlmin'),
    connect = require('gulp-connect'),
    minifyCss = require('gulp-minify-css'),
    livereload = require('gulp-livereload'),
    ejs = require("gulp-ejs");

var config = {
      activity : {
        title : "天天抽奖",
        deadtime : "2016-01-01 12:00:00",
        desc : [
          "充值任意金额即可获得1次抽奖机会！",
          "20%几率可抽中价值45元的\"迅雷白金卡3个月\"哟！"
        ],
        rules : [
          "活动时间为2015年12月31日至2016年1月4日12:00",
          "玩家每天充值可获得1次抽奖机会，但1个账号仅可中奖1次。",
          "在迅雷下载的手机游戏，登陆账号必须是迅雷账号才可参加！",
          "奖品\"迅雷白金卡3个月\"的有效使用期为2015年12月31日20:00前有效。"
        ]
      },
      game : {
        "gameid":"000001",
        "gamepy":"test",
        "gamename":"元旦",
        "gamedesc":"元旦充值",
        "gameaward":"哈哈哈",
        "downloadurl":"哈哈哈",
        "logourl":"哈哈哈",
      },
      awards : [
        {
          id : "lb",
          name : "迅雷独家礼包"
        },
        {
          id : "jd50",
          name : "50元京东卡"
        },
        {
          id : "wo",
          name : "蓝萌萌超大玩偶"
        },
        {
          id : "myaqt",
          name : "秘诱安全套"
        }
      ],
      downloadbtn : false,
      lotterybtnbtn : {},
      myawardbtn : false,
      winnerslist : false,
    };

var sftpOption = {
			host: '10.10.10.199',
			user: 'root1',
			pass: '',
      remotePath: '/data/vhosts/app_scoremall/html/activities/' + config.game.gamepy +'/'
		};

var path = {
  src : "./src",
  dist : "./dist",
  pro : "./pro/" + config.game.gamepy,
  zip : "./zip",
};

gulp.task('ejs', function () {
  return gulp.src(path.src + "/tpl.ejs")
    .pipe(ejs({
      config : config
    }))
    .pipe(rename('index.html'))
    .pipe(gulp.dest(path.dist));
});

gulp.task('js', function () {
  return gulp.src('./src/app.js')
    .pipe(rename('app.js'))
    .pipe(gulp.dest(path.dist))
    // .pipe(uglify())
    // .pipe(rename('app.min.js'))
    // .pipe(gulp.dest(path.pro))
    // .pipe(sftp(sftpOption))
    .pipe(connect.reload());
});

gulp.task('connect', function () {
  connect.server({
    root: path.dist,
    livereload: true
  });
});

gulp.task('watch', function() {
  gulp.watch('./src/*.js', ['js']);
  gulp.watch('./src/*.ejs', ['ejs']);
});

gulp.task('prod',function(){
  gulp.src('./dist/*.js')
    .pipe(uglify())
    .pipe(rename('app.min.js'))
    .pipe(gulp.dest(path.pro))
    .pipe(sftp(sftpOption));
  gulp.src('./dist/*.html')
    .pipe(rename('index.html'))
    .pipe(gulp.dest(path.pro))
    .pipe(sftp(sftpOption));
  gulp.src('./dist/*.jpg')
    .pipe(gulp.dest(path.pro))
    .pipe(sftp(sftpOption));
});

gulp.task('zip',function(){
  gulp.src(path.pro+'/')
    .pipe(zip(config.game.gamepy+'.zip'))
    .pipe(gulp.dest(path.zip));
});


gulp.task('default', ['connect', 'watch']);
gulp.task('dev', ['connect', 'watch', 'ejs', 'js']);
